package com.CST.service.impl.zx;

import com.CST.common.api.ResultObject;
import com.CST.entity.zx.ZxDataentry;
import com.CST.mapper.cst.zx.ZxDataentryMapper;
import com.CST.service.zx.IZxDataentryService;
import com.CST.util.ExcelTransfer;
import com.CST.util.PageHelperTool;
import com.CST.vo.zx.ZxDataentryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2023-02-27
 */
@Service
public class ZxDataentryServiceImpl extends ServiceImpl<ZxDataentryMapper, ZxDataentry> implements IZxDataentryService {

    @Resource
    private ZxDataentryMapper zxDataentryMapper;

    @Override
    public ResultObject<List<ZxDataentry>> page(ZxDataentry zxDataentry) {
        List<ZxDataentry> zxDataentries = zxDataentryMapper.pageForList(zxDataentry);
        return ResultObject.successReturn(
                PageHelperTool.excutePageInfo(zxDataentries, zxDataentry.getPageNo(), zxDataentry.getPageSize()));
    }

    @Override
    public ResultObject<String> saveEntry(ZxDataentry zxDataentry) {
        LambdaQueryWrapper<ZxDataentry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(zxDataentry.getEntryCode()!= null,ZxDataentry::getEntryCode,zxDataentry.getEntryCode());
        ZxDataentry one = this.getOne(lambdaQueryWrapper);
        if (one != null) {
            return ResultObject.failed("数据项编码重复");
        }
        return this.save(zxDataentry)?ResultObject.success("新增成功"):ResultObject.failed("新增失败");
    }

    @Override
    public ResultObject<String> updateEntry(ZxDataentry zxDataentry) {
        LambdaQueryWrapper<ZxDataentry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(zxDataentry.getEntryCode()!= null,ZxDataentry::getEntryCode,zxDataentry.getEntryCode())
                .ne(zxDataentry.getId()!= null,ZxDataentry::getId,zxDataentry.getId());
        ZxDataentry one = this.getOne(lambdaQueryWrapper);
        if (one != null) {
            return ResultObject.failed("数据项编码重复");
        }
        boolean success = this.updateById(zxDataentry);
        return success?ResultObject.success("修改成功"):ResultObject.failed("修改失败");
    }

    @Override
    public ResultObject<List<ZxDataentryVO>> getEntryByDictId(String dictCode) {
        LambdaQueryWrapper<ZxDataentry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dictCode!= null,ZxDataentry::getDictCode,dictCode)
                .orderByAsc(ZxDataentry::getEntryOrder)
                .eq(ZxDataentry::getEnableFlag,1)
                .select(ZxDataentry::getEntryCode,ZxDataentry::getEntryName);
        List<ZxDataentry> list = this.list(lambdaQueryWrapper);
        List<ZxDataentryVO> voList = list.stream().map(zxDataentry -> {
            ZxDataentryVO zxDataentryVO = new ZxDataentryVO(zxDataentry.getEntryCode(), zxDataentry.getEntryName());
            return zxDataentryVO;
        }).collect(Collectors.toList());
        return ResultObject.success(voList);
    }

    @Override
    public List<ZxDataentry> getEntryByEntryName(String entryName) {
        LambdaQueryWrapper<ZxDataentry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(entryName != null,ZxDataentry::getEntryName,entryName);
        List<ZxDataentry> list = this.list();
        return list;
    }

    @Override
    public boolean editById(ZxDataentry zxDataentry) {
        LambdaUpdateWrapper<ZxDataentry> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(zxDataentry.getEnableFlag() != null,ZxDataentry::getEnableFlag,zxDataentry.getEnableFlag())
                .eq(zxDataentry.getId()!=null, ZxDataentry::getId,zxDataentry.getId());
        return this.update(lambdaUpdateWrapper);
    }

    @Override
    @Transactional
    public void download(HttpServletResponse response, String entryName, String dictCode) {
        ExcelTransfer<ZxDataentry> excelTransfer = new ExcelTransfer<>();
        LambdaQueryWrapper<ZxDataentry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(entryName != null,ZxDataentry::getEntryName,entryName)
                .eq(dictCode != null,ZxDataentry::getDictCode,dictCode)
                .orderByAsc(ZxDataentry::getEntryOrder);
        List<ZxDataentry> list = this.list(lambdaQueryWrapper);
        try {
            excelTransfer.exportExcel(response,list,"数据字典","数据项", ZxDataentry.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional
    public void saveFile(MultipartFile file) {
        ExcelTransfer<ZxDataentry> excelTransfer = new ExcelTransfer<>();
//        List<ZxDataentry> zxDataentries = this.list();
        try {
            excelTransfer.importExcel(file, this,ZxDataentry.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
